package com.hcbd.tdwt.ui.haige.common.fragment;
import android.annotation.SuppressLint;
import android.widget.TextView;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseFragment;
import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.base.Listener.InfoInterface;
import com.hcbd.tdwt.util.SharedPreferencesUtil;
import com.hcbd.tdwt.util.StringUtil;
import com.pangu.bdsdk2021.entity.terminaltwo.BDGGAInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDRMCInfo;
import com.hcbd.tdwt.ui.xingyu.vo.SatelliteVO;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.text.MessageFormat;
import java.util.List;
@SuppressLint("NonConstantResourceId")
@EFragment(R.layout.fragment_position_info_haige)
public class PositionInfoFragment extends BaseFragment implements InfoInterface {
    @ViewById
    TextView longitudeTv, latitudeTv, altitudeTv, positionStatus;
    @ViewById
    TextView speedTv, directionTv, satelliteNumber, hdopTv;
    SharedPreferencesUtil sp;
    private  int format=0;
    private final String lon_first="经度：";
    private final String lat_first="纬度：";
    @AfterViews
    protected void init() {
        sp=new SharedPreferencesUtil(this.getContext());
        format=sp.getAttributeInt(Constant.LATITUDE_AND_LONGITUDE_FORMAT);

    }

    @Override
    public void onGSVInfo(List<SatelliteVO> satelliteVOS) {

    }

    @Override
    public void onGGAInfo(BDGGAInfo bdggaInfo) {
        if (longitudeTv == null) {
            return;
        }
        longitudeTv.setText(MessageFormat.format("{0}{1}", lon_first, ggaConvertLon(bdggaInfo.longitude, 3)));
        latitudeTv.setText(MessageFormat.format("{0}{1}", lat_first, ggaConvertLon(bdggaInfo.latitude, 2)));
        hdopTv.setText(MessageFormat.format("HDOP：{0}", bdggaInfo.hdop));
        String status = "无效";
        if ("1".equals(bdggaInfo.statusIndication)) {
            status = "单点定位";
        }
        altitudeTv.setText(MessageFormat.format("海拔：{0}", bdggaInfo.antennaHeight));
        positionStatus.setText(MessageFormat.format("定位状态：{0}", status));
        satelliteNumber.setText(MessageFormat.format("卫星数：{0}", bdggaInfo.visibleSatellite));

    }

    @Override
    public void onRMCInfo(BDRMCInfo bdrmcInfo) {
        if (speedTv == null) {
            return;
        }
        speedTv.setText(MessageFormat.format("速度：{0}", bdrmcInfo.groundSpeed));
        directionTv.setText(MessageFormat.format("方向：{0}", bdrmcInfo.groundHeading));
    }

    public String ggaConvertLon(String oldStr, int index) {
        String newStr = "";
        if (oldStr != null && !"".equals(oldStr)) {
            String repStr = oldStr.replace(".", "");
            String insertStr = StringUtil.stringInsert(repStr, ".", index);
            if (Constant.DEGREE_MINUTES_SECONDS_FORMAT == format) {
                newStr = StringUtil.changeToDFM(StringUtil.reservedLoc(Double.parseDouble(insertStr), 6));
            } else if (Constant.DEGREE_FORMAT == format) {
                newStr = StringUtil.reservedLocStr(Double.parseDouble(insertStr), 6);
            }
        }
        return newStr;
    }
}
